package com.example.myimageshare.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;

import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class MyPhotoAdapter extends PagerAdapter {

    private static final String TAG = MyPhotoAdapter.class.getSimpleName();
    private List<String> imgUrls;
    private AppCompatActivity activity;


    public MyPhotoAdapter(List<String> imgUrls, AppCompatActivity activity){
        this.imgUrls = imgUrls;
        this.activity = activity;
    }

    public Object instantiateItem(ViewGroup viewGroup,int position){
        String url = imgUrls.get(position);
        PhotoView photoView = new PhotoView(activity);
        Glide.with(activity)
                .load(url)
                .into(photoView);
        viewGroup.addView(photoView);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"onClick: ");
                activity.finish();
            }
        });
        return photoView;
    }

    @Override
    public int getCount() {
        //是否不为空，是返回图片URL数量，否则返回0
        return imgUrls != null ? imgUrls.size() :0;
    }

    @Override
    public void destroyItem(ViewGroup viewGroup, int position,Object object){
        viewGroup.removeView((View)object);
    }

    @Override
    public int getItemPosition(Object object){
        //解决ViewPager中调用notifyDataSetChanged失效问题
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }


}
